package com.abe.mvp.view.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abe.app.RelayoutTool;
import com.abe.mvp.model.bean.MainTopBean;
import com.abe.threadmill.R;

@SuppressLint({ "InflateParams", "ClickableViewAccessibility" })
public class MainTopAdapter extends BaseAdapter {
	private List<MainTopBean> data;
	private LayoutInflater layoutInflater;
	private float scale;

	public MainTopAdapter(Context context, List<MainTopBean> data, float scale) {
		this.data = data;
		this.scale = scale;
		this.layoutInflater = LayoutInflater.from(context);
	}

	public final class ViewHolder {
		public LinearLayout layout;
		public TextView name, count;
		public View divider;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	/**
	 * 获得某一位置的数
	 */
	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	/**
	 * 获得唯一标识
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			// 获得组件，实例化组件
			convertView = layoutInflater.inflate(
					R.layout.item_list_main_top_info, null);
			RelayoutTool.relayoutViewHierarchy(convertView, scale);
			viewHolder.layout = (LinearLayout) convertView
					.findViewById(R.id.item_top_layout);
			viewHolder.name = (TextView) convertView
					.findViewById(R.id.item_top_name);
			viewHolder.count = (TextView) convertView
					.findViewById(R.id.item_top_count);
			viewHolder.divider = convertView
					.findViewById(R.id.item_common_divider_vertical);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		MainTopBean bean = data.get(position);
		viewHolder.name.setText(bean.getName());
		viewHolder.count.setText(bean.getData());
		if (position == data.size() - 1) {
			viewHolder.divider.setVisibility(View.GONE);
		} else {
			viewHolder.divider.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

}
